--
-- Author: yawei.zhu
-- Date: 2018-09-03 14:30:38
--
-- DungeonDropItemRender
--
local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DungeonDropItemNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())

    self._ui.Panel_qq:setVisible(false)
    self._ui["Panel/Panel_qq/wx"]:setVisible(false)
    self._ui["Panel/Panel_qq/qq"]:setVisible(false)
    self._ui["Panel/Panel_qq/vip_super"]:setVisible(false)
    self._ui["Panel/Panel_qq/vip_qq"]:setVisible(false)
    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end

    local index = self._data[1]
    local loot = self._data[2]
    local rootPanel = self._ui.Panel
    local bgNode = rootPanel:getChildByName("bg")
    if index % 2 == 0 then
        bgNode:setVisible(false)
    else
        bgNode:setVisible(true)
    end
    local itemData = UD:parseLootToBagItem(loot)
    local iconNode = rootPanel:getChildByName("icon")
    local nameText = rootPanel:getChildByName("name")
    local jobText = rootPanel:getChildByName("Text")
    local scoreText = rootPanel:getChildByName("score")

    local bg_score = rootPanel:getChildByName("bg_score")
    local tlt_score = rootPanel:getChildByName("tlt_score")

    local function hideScore()
        bg_score:setVisible(false)
        tlt_score:setVisible(false)
        scoreText:setVisible(false)
    end

    local quality = itemData.quality
    if quality then
        local color = Enums.QualityColor[tonumber(quality)]
        if color then
            nameText:setTextColor(color)
        end
    end

    nameText:setString(itemData:getName())

    local uiItem = IconRender.create(iconNode)
    uiItem:setData(itemData)
    uiItem:setTipEnable(true)


    if itemData.itemBagType == Enums.ItemBagType.equip then
        uiItem:setNumBgVisible(false)
        local desStr = itemData:getUserShow() .. " " .. itemData:getPosName()
        jobText:setString(desStr)
        scoreText:setString(itemData:getGearScore())
    elseif itemData.itemBagType == Enums.ItemBagType.jewel then
        uiItem:setNumBgVisible(false)
        jobText:setString(itemData:getTypeName())
        hideScore()
    else--if itemData.itemBagType == Enums.ItemBagType.item then
        uiItem:setNumBgVisible(true)
        jobText:setString(itemData:getTypeName())
        hideScore()
    end

end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

function ClassRef:setExtra(extra)
    if not extra then return end
    if not UD.msdkData:isEnableShowBonus() then return end
    self._ui.Panel_qq:setVisible(true)
    if extra.type == "Tencent" then -- 手Q游戏中心启动
        self._ui["Panel/Panel_qq/qq"]:setVisible(true)
        self._ui["Panel/Panel_qq/qq/num"]:setString("+"..extra.num.."%")
    elseif extra.type == "WeChat" then -- 微信游戏中心启动
        self._ui["Panel/Panel_qq/wx"]:setVisible(true)
        self._ui["Panel/Panel_qq/wx/num"]:setString("+"..extra.num.."%")
    elseif extra.type == "SuperVIP" then -- 手Q超级会员
        self._ui["Panel/Panel_qq/vip_super"]:setVisible(true)
        self._ui["Panel/Panel_qq/vip_super/num"]:setString("+"..extra.num.."%")
    elseif extra.type == "NormalVIP" then --手Q普通会员
        self._ui["Panel/Panel_qq/vip_qq"]:setVisible(true)
        self._ui["Panel/Panel_qq/vip_qq/num"]:setString("+"..extra.num.."%")
    else
        self._ui.Panel_qq:setVisible(false)
    end
end

return ClassRef
